package com.asion.vmovie.ui.MVPModel;

import com.asion.vmovie.api.ApiService;
import com.asion.vmovie.beans.SeriesDetialsBean;
import com.asion.vmovie.beans.VideoBean;
import com.asion.vmovie.ui.MVPContract.XiLieDetialContract;
import com.asion.vmovie.utils.retrofitCache.SetCacheUtils;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by asion on 16-10-21.
 */

public class SeriesModel implements XiLieDetialContract.Model {
    @Override
    public void getSeriesDetial(int seriesid, XiLieDetialContract.OnGetSeriesData onGetSeriesData) {
        Observable<SeriesDetialsBean> xiLieDetials = new Retrofit.Builder().addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .client(SetCacheUtils.setCache())
                .baseUrl(ApiService.BASE_URL)
                .build()
                .create(ApiService.class)
                .getXiLieDetials(seriesid);

        xiLieDetials.subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(seriesDetialsBean -> onGetSeriesData.onGetSuccess(seriesDetialsBean)
                ,throwable -> onGetSeriesData.onGetError(throwable));
    }

    @Override
    public void getVideo(int series_postid, XiLieDetialContract.OnGetVideoData onGetVideoData) {
        Observable<VideoBean> xiLieVideos = new Retrofit.Builder().addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .client(SetCacheUtils.setCache())
                .baseUrl(ApiService.BASE_URL)
                .build()
                .create(ApiService.class)
                .getXiLieVideos(series_postid);

        xiLieVideos.subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(videoBean -> onGetVideoData.onGetSuccess(videoBean)
                ,throwable -> onGetVideoData.onGetError(throwable));
    }
}
